home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 321 / compsrc3 / symout.c < prev   
Encoding:
C/C++ Source or Header  |  1988-10-20  |  30.4 KB  |  1,128 lines

  1. /* Output GDB-format symbol table information from GNU compiler.
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY.  No author or distributor
  8. accepts responsibility to anyone for the consequences of using it
  9. or for whether it serves any particular purpose or works at all,
  10. unless he says so in writing.  Refer to the GNU CC General Public
  11. License for full details.
  12.  
  13. Everyone is granted permission to copy, modify and redistribute
  14. GNU CC, but only under the conditions described in the
  15. GNU CC General Public License.   A copy of this license is
  16. supposed to have been given to you along with GNU CC so you
  17. can know your rights and responsibilities.  It should be in a
  18. file named COPYING.  Among other things, the copyright notice
  19. and this notice must be preserved on all copies.  */
  20.  
  21.  
  22. #include "config.h"
  23. #include "tree.h"
  24. #include "symseg.h"
  25. #include "rtl.h"
  26. #include "gdbfiles.h"
  27. #include <stdio.h>
  28.  
  29. /* Get N_SO from stab.h if we can expect the file to exist.  */
  30. #ifdef DBX_DEBUGGING_INFO
  31. #include <stab.h>
  32. #endif
  33.  
  34. /* .stabs code for source file name.  */
  35. #ifndef N_SO
  36. #define    N_SO 0x64
  37. #endif
  38.  
  39. /* Unix maximum on file name length.  Needed for getwd.  */
  40. #define MAXNAMLEN 1024
  41.  
  42. /* Get the number to output for a reference to type TYPE.  */
  43. #define TYPE_OUTPUT_ADDRESS(TYPE) \
  44.   TYPE_SYMTAB_ADDRESS (TYPE_MAIN_VARIANT (TYPE))
  45.  
  46. /* Stream for writing symbol table file.  */
  47. static FILE *symfile;
  48.  
  49. /* Name of symbol table file.  */
  50. static char *symfile_name;
  51.  
  52. /* Stream for writing to assembler file.  */
  53. static FILE *asmfile;
  54.  
  55. /* Address for allocating space in symbol table file.
  56.    Changes in this variable are paired globally with writes to symfile,
  57.    but often we allocate many structures, advancing next_address,
  58.    before writing any of them.  */
  59. static int next_address;
  60.  
  61. /* Chain recording all the types that have been output,
  62.    giving the address-in-the-symseg of each one.  */
  63.  
  64. struct typevec_elt
  65. {
  66.   int address;
  67.   struct typevec_elt *next;
  68. };
  69.  
  70. static struct typevec_elt *typevec;
  71.  
  72. /* Number of types recorded so far in the chain.  */
  73.  
  74. static int total_types;
  75.  
  76. /* `blockvec' is a chain recording all the symbol-blocks that have been output,
  77.    giving the address-in-the-symseg of each one.  */
  78.  
  79. struct blockvec_elt
  80. {
  81.   int address;
  82.   struct blockvec_elt *next;
  83. };
  84.  
  85. static struct blockvec_elt *blockvec;
  86.  
  87. /* Number of blocks recorded so far in the chain.  */
  88.  
  89. static int total_blocks;
  90.  
  91. static void symout_range_bounds ();
  92. static void symout_array_domain ();
  93. static void symout_record_fields ();
  94. static void symout_enum_values ();
  95. static void symout_record_field_names ();
  96. static void symout_enum_value_names ();
  97. static int subrange_p ();
  98. static void symout_strings_skip ();
  99. static void symout_strings_print ();
  100.  
  101. /* At the beginning of compilation, start writing the symbol table.
  102.    Initialize the type and block chain.
  103.    Also open and initialize the symseg file.  */
  104.  
  105. void
  106. symout_init (filename, asm_file, sourcename)
  107.      char *filename;
  108.      FILE *asm_file;
  109.      char *sourcename;
  110. {
  111.   struct symbol_root buffer;
  112.  
  113. #ifdef VMS
  114.   fatal ("Cannot write GDB debugging format on VMS");
  115. #endif
  116.  
  117.   asmfile = asm_file;
  118.   fprintf (asmfile, ".text 0\n.gdbbeg 0\n.gdbbeg 1\n");
  119.   fprintf (asmfile,
  120.        "Ltext:\t.stabs \"%s\",%d,0,0,Ltext\n",
  121.        sourcename, N_SO);
  122.   fprintf (asmfile, ".data 0\nLdata:\n");
  123.   ASM_OUTPUT_LOCAL (asmfile, "Lbss", 0);
  124.   fprintf (asmfile, ".gdbsym Ldata,%d\n",
  125.        (char *) &buffer.databeg - (char *) &buffer);
  126.   fprintf (asmfile, ".gdbsym Lbss,%d\n",
  127.        (char *) &buffer.bssbeg - (char *) &buffer);
  128.  
  129.   symfile = fopen (filename, "w");
  130.   if (symfile == 0)
  131.     pfatal_with_name (symfile);
  132.   symfile_name = (char *) malloc (strlen (filename) + 1);
  133.   strcpy (symfile_name, filename);
  134.  
  135.   typevec = 0;
  136.   blockvec = 0;
  137.   total_types = 0;
  138.   total_blocks = 0;
  139.  
  140.   bzero (&buffer, sizeof buffer);
  141.   fwrite (&buffer, sizeof buffer, 1, symfile);
  142.  
  143.   next_address = sizeof buffer;
  144. }
  145.  
  146. /* Functions for outputting strings into the symbol table.
  147.    The string to be output is effectively the concatenation of
  148.    the two strings P1 and P2.  Their lengths are given as S1 and S2.
  149.    If P1 or P2 is zero, that string is not used.
  150.  
  151.    A null character is output to terminate the string,
  152.    and it is followed by more nulls as padding to a word boundary.  */
  153.  
  154. static void
  155. symout_strings (p1, s1, p2, s2)
  156.      char *p1;
  157.      int s1;
  158.      char *p2;
  159.      int s2;
  160. {
  161.   symout_strings_print (p1, s1, p2, s2);
  162.   symout_strings_skip (p1, s1, p2, s2);
  163. }
  164.  
  165. /* Like symout_strings but only output; do not update next_address.  */
  166.  
  167. static void
  168. symout_strings_print (p1, s1, p2, s2)
  169.      char *p1;
  170.      int s1;
  171.      char *p2;
  172.      int s2;
  173. {
  174.   register int total;
  175.  
  176.   if (p1 && s1 == 0)
  177.     s1 = strlen (p1);
  178.   if (p2 && s2 == 0)
  179.     s2 = strlen (p2);
  180.  
  181.   if (p1)
  182.     fwrite (p1, s1, 1, symfile);
  183.   if (p2)
  184.     fwrite (p2, s2, 1, symfile);
  185.   putc (0, symfile);
  186.  
  187.   total = s1 + s2 + 1;
  188.   while (total % sizeof (int))
  189.     {
  190.       putc (0, symfile);
  191.       total++;
  192.     }
  193. }
  194.  
  195. /* Like symout_strings but just update next_address; do not output.  */
  196.  
  197. static void
  198. symout_strings_skip (p1, s1, p2, s2)
  199.      char *p1;
  200.      int s1;
  201.      char *p2;
  202.      int s2;
  203. {
  204.   register int total;
  205.  
  206.   if (p1 && s1 == 0)
  207.     s1 = strlen (p1);
  208.   if (p2 && s2 == 0)
  209.     s2 = strlen (p2);
  210.  
  211.   total = s1 + s2 + 1;
  212.   while (total % sizeof (int))
  213.     total++;
  214.  
  215.   next_address += total;
  216. }
  217.  
  218. /* Call here to output a chain of types.
  219.    After each function, this is done first for the chain of permanent types
  220.    made during the function, and then for the chain of temporary types.
  221.    This must be done before outputting the symbols and blocks of the function.
  222.  
  223.    At the end of compilation, this is done for all the permanent types
  224.    made since the last function.
  225.  
  226.    Each permanent type is done once, at the beginning of the next function,
  227.    or at the end of the compilation if no functions follow.
  228.    Once a type has been processed here, its TYPE_SYMTAB_ADDRESS remains
  229.    set up.  */
  230.  
  231. void
  232. symout_types (types)
  233.      tree types;
  234. {
  235.   struct typerec
  236.   {
  237.     int number;
  238.     int address;
  239.     int nfields;
  240.     int fields_address;
  241.     int name_address;
  242.     char *name;
  243.     char *name_prefix;
  244.   };
  245.  
  246.   register int n_types, i;
  247.   register struct typerec *records;
  248.   register tree next;
  249.   struct type buffer;
  250.  
  251.   for (next = types, n_types = 0;
  252.        next;
  253.        next = TREE_CHAIN (next), n_types++);
  254.  
  255.   records = (struct typerec *) alloca (n_types * sizeof (struct typerec));
  256.  
  257.   for (next = types, i = 0;
  258.        next;
  259.        next = TREE_CHAIN (next), i++)
  260.     {
  261.       register struct typevec_elt *velt
  262.     = (struct typevec_elt *) xmalloc (sizeof (struct typevec_elt));
  263.       velt->next = typevec;
  264.       typevec = velt;
  265.  
  266.       total_types++;
  267.  
  268.       if (TYPE_NAME (next))
  269.     {
  270.       records[i].name_address = next_address;
  271.  
  272.       if (TREE_CODE (TYPE_NAME (next)) == IDENTIFIER_NODE)
  273.         {
  274.           records[i].name = IDENTIFIER_POINTER (TYPE_NAME (next));
  275.           switch (TREE_CODE (next))
  276.         {
  277.         case RECORD_TYPE:
  278.           records[i].name_prefix = "struct ";
  279.           break;
  280.  
  281.         case UNION_TYPE:
  282.           records[i].name_prefix = "union ";
  283.           break;
  284.  
  285.         case ENUMERAL_TYPE:
  286.           records[i].name_prefix = "enum ";
  287.           break;
  288.         }
  289.         }
  290.       else
  291.         {
  292.           records[i].name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (next)));
  293.           records[i].name_prefix = 0;
  294.         }
  295.       symout_strings_skip (records[i].name_prefix, 0,
  296.                    records[i].name, 0);
  297.  
  298.     }
  299.       else
  300.     {
  301.       records[i].name = 0;
  302.       records[i].name_address = 0;
  303.       records[i].name_prefix = 0;
  304.     }
  305.  
  306.       records[i].address = next_address;
  307.       TYPE_SYMTAB_ADDRESS (next) = next_address;
  308.       velt->address = next_address;
  309.       next_address += sizeof (struct type);
  310.       records[i].nfields = 0;
  311.       records[i].fields_address = 0;
  312.       switch (TREE_CODE (next))
  313.     {
  314.     case ARRAY_TYPE:
  315.       records[i].nfields
  316.         = (TYPE_DOMAIN(next)
  317.            ? ! integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (next)))
  318.            : 0 );
  319.       break;
  320.  
  321.     case INTEGER_TYPE:
  322.       if (subrange_p (next))
  323.         buffer.nfields = 2;
  324.       break;
  325.  
  326.     case RECORD_TYPE:
  327.     case UNION_TYPE:
  328.     case ENUMERAL_TYPE:
  329.       records[i].nfields = list_length (TYPE_FIELDS (next));
  330.     }
  331.       if (records[i].nfields)
  332.     records[i].fields_address = next_address;
  333.       next_address += records[i].nfields * sizeof (struct field);
  334.     }
  335.  
  336.   for (next = types, i = 0;
  337.        next;
  338.        next = TREE_CHAIN (next), i++)
  339.     {
  340.       if (records[i].name)
  341.     symout_strings_print (records[i].name_prefix, 0,
  342.                   records[i].name, 0);
  343.  
  344.       if (TYPE_SIZE (next) == 0)
  345.     buffer.length = 0;
  346.       else
  347.     buffer.length
  348.       = (TREE_INT_CST_LOW (TYPE_SIZE (next))
  349.          * TYPE_SIZE_UNIT (next) / BITS_PER_UNIT);
  350.       buffer.name = (char *) records[i].name_address;
  351.       buffer.target_type = (struct type *) (TREE_TYPE (next) ? TYPE_OUTPUT_ADDRESS (TREE_TYPE (next)) : 0);
  352.  
  353.       buffer.pointer_type = 0;
  354.       buffer.function_type = 0;
  355.       buffer.flags
  356.     = ((TREE_CODE (next) == INTEGER_TYPE || TREE_CODE (next) == ENUMERAL_TYPE)
  357.        && TREE_UNSIGNED (next))
  358.       ? TYPE_FLAG_UNSIGNED : 0;
  359.       buffer.nfields = records[i].nfields;
  360.       buffer.fields = (struct field *) records[i].fields_address;
  361.  
  362.       switch (TREE_CODE (next))
  363.     {
  364.     case INTEGER_TYPE:
  365.       buffer.code = TYPE_CODE_INT;
  366.       if (buffer.nfields)
  367.         buffer.code = TYPE_CODE_RANGE;
  368.       break;
  369.  
  370.     case REAL_TYPE:
  371.       buffer.code = TYPE_CODE_FLT;
  372.       break;
  373.  
  374.     case VOID_TYPE:
  375.       buffer.code = TYPE_CODE_VOID;
  376.       break;
  377.  
  378.     case POINTER_TYPE:
  379.       buffer.code = TYPE_CODE_PTR;
  380.       break;
  381.  
  382.     case ARRAY_TYPE:
  383.       if (buffer.nfields == 0)
  384.         buffer.code = TYPE_CODE_ARRAY;
  385.       else
  386.         buffer.code = TYPE_CODE_PASCAL_ARRAY;
  387.       break;
  388.  
  389.     case RECORD_TYPE:
  390.       buffer.code = TYPE_CODE_STRUCT;
  391.       break;
  392.  
  393.     case UNION_TYPE:
  394.       buffer.code = TYPE_CODE_UNION;
  395.       break;
  396.  
  397.     case FUNCTION_TYPE:
  398.       buffer.code = TYPE_CODE_FUNC;
  399.       break;
  400.  
  401.     case ENUMERAL_TYPE:
  402.       buffer.code = TYPE_CODE_ENUM;
  403.       break;
  404.  
  405.     default:
  406.       abort ();
  407.     }
  408.  
  409.       fwrite (&buffer, sizeof buffer, 1, symfile);
  410.  
  411.       switch (TREE_CODE (next))
  412.     {
  413.     case ARRAY_TYPE:
  414.       if (buffer.nfields)
  415.         symout_array_domain (next);
  416.       break;
  417.  
  418.     case RECORD_TYPE:
  419.     case UNION_TYPE:
  420.       symout_record_fields (next);
  421.       break;
  422.  
  423.     case ENUMERAL_TYPE:
  424.       symout_enum_values (next);
  425.       break;
  426.  
  427.     case INTEGER_TYPE:
  428.       if (buffer.nfields)
  429.         symout_range_bounds (next);
  430.     }
  431.     }
  432.  
  433.   for (next = types, i = 0;
  434.        next;
  435.        next = TREE_CHAIN (next), i++)
  436.     {
  437.       switch (TREE_CODE (next))
  438.     {
  439.     case RECORD_TYPE:
  440.     case UNION_TYPE:
  441.       symout_record_field_names (next);
  442.       break;
  443.  
  444.     case ENUMERAL_TYPE:
  445.       symout_enum_value_names (next);
  446.       break;
  447.     }
  448.     }
  449. }
  450.  
  451. /* Return nonzero if TYPE's range of possible values
  452.    is not the full range allowed by the number of bits it has.
  453.    TYPE is assumed to be an INTEGER_TYPE or ENUMERAL_TYPE.  */
  454.  
  455. static int
  456. subrange_p (type)
  457.      tree type;
  458. {
  459.   int uns = TREE_UNSIGNED (type);
  460.  
  461.   if (TYPE_PRECISION (type) >= HOST_BITS_PER_INT)
  462.     {
  463.       if (uns)
  464.     return integer_zerop (TYPE_MIN_VALUE (type))
  465.       && TREE_INT_CST_LOW (TYPE_MAX_VALUE (type)) == 0
  466.         && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (type))
  467.         == (1 << (TYPE_PRECISION (type) - HOST_BITS_PER_INT)) - 1);
  468.       return TREE_INT_CST_LOW (TYPE_MIN_VALUE (type)) == 0
  469.     && TREE_INT_CST_LOW (TYPE_MAX_VALUE (type)) == 0
  470.       && (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type))
  471.           == (-1) << (TYPE_PRECISION (type) - 1 - HOST_BITS_PER_INT))
  472.         && (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (type))
  473.         == (1 << (TYPE_PRECISION (type) - 1 - HOST_BITS_PER_INT)) - 1);
  474.     }
  475.  
  476.   if (uns)
  477.     {
  478.       int mask;
  479.  
  480.       if (TYPE_PRECISION (type) == HOST_BITS_PER_INT)
  481.     /* Shifting by 32 loses on some machines.  */
  482.     mask = -1;
  483.       else
  484.     mask = (1 << TYPE_PRECISION (type)) - 1;
  485.  
  486.       return (integer_zerop (TYPE_MIN_VALUE (type))
  487.           && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (type)) == mask));
  488.     }
  489.   else
  490.     return ((TREE_INT_CST_LOW (TYPE_MIN_VALUE (type))
  491.          == (-1) << (TYPE_PRECISION (type) - 1))
  492.         && (TREE_INT_CST_LOW (TYPE_MAX_VALUE (type))
  493.         == (1 << (TYPE_PRECISION (type) - 1)) - 1));
  494. }
  495.  
  496. /* Functions to output the "fields" of various kinds of types.
  497.    These assume that next_address has already been incremented to
  498.    cover these fields, and the fields of all the other types being
  499.    output in this batch; so next_address can be used to allocate
  500.    space to store field names, etc.  */
  501.  
  502. static void
  503. symout_array_domain (type)
  504.      tree type;
  505. {
  506.   struct field buffer;
  507.  
  508.   buffer.bitpos = 0;
  509.   buffer.bitsize = 0;
  510.   buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TYPE_DOMAIN (type));
  511.   buffer.name = 0;
  512.   fwrite (&buffer, sizeof (struct field), 1, symfile);
  513. }
  514.  
  515. static void
  516. symout_range_bounds (type)
  517.      tree type;
  518. {
  519.   struct field buffer;
  520.  
  521.   buffer.bitpos = TREE_INT_CST_LOW (TYPE_MIN_VALUE (type));
  522.   buffer.bitsize = 0;
  523.   buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (type);
  524.   buffer.name = 0;
  525.   fwrite (&buffer, sizeof (struct field), 1, symfile);
  526.  
  527.   buffer.bitpos = TREE_INT_CST_LOW (TYPE_MAX_VALUE (type));
  528.   buffer.bitsize = 0;
  529.   buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (type);
  530.   buffer.name = 0;
  531.   fwrite (&buffer, sizeof (struct field), 1, symfile);
  532. }
  533.  
  534. static void
  535. symout_record_fields (type)
  536.      tree type;
  537. {
  538.   struct field buffer;
  539.   register tree field;
  540.  
  541.   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  542.     {
  543.       buffer.bitpos = DECL_OFFSET (field);
  544.       buffer.bitsize
  545.     = (TREE_PACKED (field)
  546.        ? TREE_INT_CST_LOW (DECL_SIZE (field)) * DECL_SIZE_UNIT (field)
  547.        : 0);
  548.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TREE_TYPE (field));
  549.       if (DECL_NAME (field))
  550.     {
  551.       buffer.name = (char *) next_address;
  552.       symout_strings_skip (0, IDENTIFIER_LENGTH (DECL_NAME (field)), 0, 0);
  553.     }
  554.       else
  555.     buffer.name = 0;
  556.       fwrite (&buffer, sizeof (struct field), 1, symfile);
  557.     }
  558. }
  559.  
  560. static void
  561. symout_enum_values (type)
  562.      tree type;
  563. {
  564.   struct field buffer;
  565.   register tree link, value;
  566.  
  567.   for (link = TYPE_VALUES (type); link; link = TREE_CHAIN (link))
  568.     {
  569.       value = TREE_VALUE (link);
  570.       buffer.bitpos = TREE_INT_CST_LOW (value);
  571.       buffer.bitsize = 0;
  572.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (type);
  573.       buffer.name = (char *) next_address;
  574.       symout_strings_skip (0, IDENTIFIER_LENGTH (TREE_PURPOSE (link)), 0, 0);
  575.       fwrite (&buffer, sizeof buffer, 1, symfile);
  576.     }
  577. }
  578.  
  579. /* Output field names or value names for the fields of a type.
  580.    This is called, for the types that need it, after the fields
  581.    have been output for all the types in the batch.
  582.    We do not update next_address here, because it has already been 
  583.    updated for all the names in all the fields in all the types.  */
  584.  
  585. static void
  586. symout_record_field_names (type)
  587.      tree type;
  588. {
  589.   register tree field;
  590.  
  591.   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  592.     if (DECL_NAME (field))
  593.       symout_strings_print (IDENTIFIER_POINTER (DECL_NAME (field)),
  594.                 IDENTIFIER_LENGTH (DECL_NAME (field)),
  595.                 0, 0);
  596. }
  597.  
  598. static void
  599. symout_enum_value_names (type)
  600.      tree type;
  601. {
  602.   register tree value;
  603.  
  604.   for (value = TYPE_VALUES (type); value; value = TREE_CHAIN (value))
  605.     symout_strings_print (IDENTIFIER_POINTER (TREE_PURPOSE (value)),
  606.               IDENTIFIER_LENGTH (TREE_PURPOSE (value)),
  607.               0, 0);
  608. }
  609.  
  610. /* Output the symbols of a block, given the list of decl nodes.
  611.    Store the file addresses at which the symbols are output
  612.    into ADDR_BUFFER, a vector which has just the right length.
  613.  
  614.    If FILTER is 1, do only the private symbols in DECLS.
  615.    If FILTER is 2, do only the public ones (but no externals).
  616.    If FILTER is 0, do all (except external functions).  */
  617.  
  618. static void
  619. symout_block_symbols (decls, addr_buffer, filter)
  620.      tree decls;
  621.      int *addr_buffer;
  622.      int filter;
  623. {
  624.   register tree decl;
  625.   struct symbol buffer;
  626.   register int i;
  627.  
  628.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  629.     {
  630.       register name_address = next_address;
  631.  
  632.       if (filter == (TREE_PUBLIC (decl) ? 1 : 2))
  633.     continue;
  634.  
  635.       /* Do not mention external functions.
  636.      Let their own files mention them.
  637.      In the top blocks, don't mention external anything.  */
  638.  
  639.       if (TREE_EXTERNAL (decl)
  640.       && (filter || TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE))
  641.     continue;
  642.  
  643.       if (TREE_TYPE (decl) == error_mark_node)
  644.     continue;
  645.  
  646.       symout_strings (IDENTIFIER_POINTER (DECL_NAME (decl)),
  647.               IDENTIFIER_LENGTH (DECL_NAME (decl)),
  648.               0, 0);
  649.       addr_buffer[i] = next_address;
  650.       buffer.name = (char *) name_address;
  651.       buffer.namespace = VAR_NAMESPACE;
  652.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TREE_TYPE (decl));
  653.       switch (TREE_CODE (decl))
  654.     {
  655.     case PARM_DECL:
  656.       buffer.class = LOC_ARG;
  657.       buffer.value.value = DECL_OFFSET (decl) / BITS_PER_UNIT;
  658.       break;
  659.  
  660.     case VAR_DECL:
  661.     case RESULT_DECL:
  662.       if (TREE_STATIC (decl) || TREE_EXTERNAL (decl))
  663.         {
  664.           if (! TREE_PUBLIC (decl) || DECL_INITIAL (decl))
  665.         {
  666.           char *str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
  667.           fprintf (asmfile, "\t.gdbsym ");
  668.           ASM_OUTPUT_LABELREF (asmfile, str);
  669.           fprintf (asmfile, ",%d\n",
  670.                next_address + (char *)&buffer.value - (char *)&buffer);
  671.           buffer.class = LOC_STATIC;
  672.         }
  673.           else
  674.         /* Uninitialized public symbols are output as .comm;
  675.            Tell GDB to get address from loader global symbol.
  676.            Also come here for symbols declared extern.  */
  677.         buffer.class = LOC_EXTERNAL;
  678.         }
  679.       else
  680.         {
  681.           if (GET_CODE (DECL_RTL (decl)) == REG)
  682.         {
  683.           buffer.class = LOC_REGISTER;
  684.           buffer.value.value = REGNO (DECL_RTL (decl));
  685.           /* Detect vars that were optimized entirely away.  */
  686.           if (buffer.value.value == -1)
  687.             buffer.class = LOC_CONST;
  688.         }
  689.           /* Locals in memory are expected to be addressed as
  690.          (PLUS (REG ...) (CONST_INT ...)).
  691.          Bomb out if that is not so.  */
  692.           else if (GET_CODE (DECL_RTL (decl)) == MEM)
  693.         {
  694.           register rtx addr = XEXP (DECL_RTL (decl), 0);
  695.           if (GET_CODE (addr) != PLUS && GET_CODE (addr) != MINUS)
  696.             abort ();
  697.           if (GET_CODE (XEXP (addr, 1)) != CONST_INT)
  698.             abort ();
  699.           buffer.class = LOC_LOCAL;
  700.           buffer.value.value = INTVAL (XEXP (addr, 1));
  701.           if (GET_CODE (addr) == MINUS)
  702.             buffer.value.value = - buffer.value.value;
  703.         }
  704.           else
  705.         abort ();
  706.         }
  707.       break;
  708.  
  709.     case TYPE_DECL:
  710.       buffer.class = LOC_TYPEDEF;
  711.       buffer.value.value = 0;
  712.       break;
  713.  
  714.     case CONST_DECL:
  715.       buffer.class = LOC_CONST;
  716.       buffer.value.value = TREE_INT_CST_LOW (DECL_INITIAL (decl));
  717.       break;
  718.  
  719.     case FUNCTION_DECL:
  720.       if (DECL_INITIAL (decl))
  721.         {
  722.           buffer.class = LOC_BLOCK;
  723.           buffer.value.value = DECL_BLOCK_SYMTAB_ADDRESS (decl);
  724.         }
  725.       else
  726.         buffer.class = LOC_EXTERNAL;
  727.     }
  728.  
  729.       fwrite (&buffer, sizeof buffer, 1, symfile);
  730.       next_address += sizeof buffer;
  731.       i++;
  732.     }
  733. }
  734.  
  735. /* Output the tags (struct, union and enum definitions) for a block,
  736.    given a list of them (a chain of TREE_LIST nodes) in TAGS.
  737.    Store their addresses in the file into ADDR_BUFFER.  */
  738.  
  739. static void
  740. symout_block_tags (tags, addr_buffer)
  741.      tree tags;
  742.      int *addr_buffer;
  743. {
  744.   register tree tag;
  745.   struct symbol buffer;
  746.   register int i;
  747.  
  748.   for (tag = tags, i = 0; tag; tag = TREE_CHAIN (tag), i++)
  749.     {
  750.       buffer.name = (char *) next_address;
  751.  
  752.       symout_strings (IDENTIFIER_POINTER (TREE_PURPOSE (tag)),
  753.               IDENTIFIER_LENGTH (TREE_PURPOSE (tag)),
  754.               0, 0);
  755.       addr_buffer[i] = next_address;
  756.       buffer.namespace = STRUCT_NAMESPACE;
  757.       buffer.type = (struct type *) TYPE_OUTPUT_ADDRESS (TREE_VALUE (tag));
  758.       buffer.class = LOC_TYPEDEF;
  759.       buffer.value.value = 0;
  760.  
  761.       fwrite (&buffer, sizeof buffer, 1, symfile);
  762.       next_address += sizeof buffer;
  763.     }
  764. }
  765.  
  766. /* Output all the data structure for a "block"
  767.    (any binding contour).
  768.    DECLS is the chain of declarations of variables in this block.
  769.    TAGS is the list of struct, union and enum tag definitions of this block.
  770.    SUPERBLOCK_ADDRESS is the symtab file address of the containing block's
  771.    data structure.  */
  772.  
  773. int
  774. symout_block (decls, tags, args, superblock_address)
  775.      tree decls;
  776.      tree tags;
  777.      tree args;
  778.      int superblock_address;
  779. {
  780.   register tree decl;
  781.   register int i;
  782.   register int *addr_buffer;
  783.   struct block buffer;
  784.   int n_decls, n_tags, n_args, total;
  785.   register struct blockvec_elt *velt;
  786.   int block_address;
  787.  
  788.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  789.     if (! TREE_EXTERNAL (decl)
  790.     || TREE_CODE (TREE_TYPE (decl)) != FUNCTION_TYPE)
  791.       i++;
  792.  
  793.   n_decls = i;
  794.  
  795.   for (decl = args, i = 0; decl; decl = TREE_CHAIN (decl), i++);
  796.   n_args = i;
  797.  
  798.   for (decl = tags, i = 0; decl; decl = TREE_CHAIN (decl), i++);
  799.   n_tags = i;
  800.  
  801.   total = n_decls + n_args + n_tags;
  802.  
  803.   addr_buffer = (int *) alloca (total * sizeof (int));
  804.  
  805.   symout_block_symbols (args, addr_buffer, 0);
  806.   symout_block_symbols (decls, addr_buffer + n_args, 0);
  807.   symout_block_tags (tags, addr_buffer + n_decls + n_args);
  808.  
  809.   velt = (struct blockvec_elt *) xmalloc (sizeof (struct blockvec_elt));
  810.   velt->next = blockvec;
  811.   velt->address = next_address;
  812.   blockvec = velt;
  813.  
  814.   buffer.startaddr = 0;
  815.   buffer.endaddr = 0;
  816.   buffer.superblock = (struct block *) superblock_address;
  817.   buffer.function = 0;
  818.   buffer.nsyms = total;
  819.  
  820.   block_address = next_address;
  821.   fwrite (&buffer, sizeof buffer - sizeof buffer.sym, 1, symfile);
  822.   next_address += sizeof buffer - sizeof buffer.sym;
  823.  
  824.   fwrite (addr_buffer, sizeof (int), total, symfile);
  825.   next_address += total * sizeof (int);
  826.  
  827.   fprintf (asmfile, "\t.gdbblock %d,%d\n", total_blocks + 2, block_address);
  828.   total_blocks++;
  829.  
  830.   return block_address;
  831. }
  832.  
  833. /* Walk STMT, the body of a function, and output symtab data on
  834.    all the blocks that compose it and all symbols inside them.
  835.    ARGS is a chain of decls for argument variables of the function.
  836.    SUPERBLOCK_ADDRESS is the address of symbol data for the
  837.    innermost block containing STMT; it is used for recursive calls,
  838.    and is always 0 for the outermost call (since the containing
  839.    block for a function is output later than the function).  */
  840.  
  841. int
  842. symout_function (stmt, args, superblock_address)
  843.      register tree stmt;
  844.      tree args;
  845.      int superblock_address;
  846. {
  847.   int address = superblock_address;
  848.  
  849.   while (stmt)
  850.     {
  851.       switch (TREE_CODE (stmt))
  852.     {
  853.     case COMPOUND_STMT:
  854.     case LOOP_STMT:
  855.       symout_function (STMT_BODY (stmt), 0, address);
  856.       break;
  857.  
  858.     case IF_STMT:
  859.       symout_function (STMT_THEN (stmt), 0, address);
  860.       symout_function (STMT_ELSE (stmt), 0, address);
  861.       break;
  862.  
  863.     case LET_STMT:
  864.       address =
  865.         symout_block (STMT_VARS (stmt), STMT_TYPE_TAGS (stmt), args,
  866.               superblock_address);
  867.  
  868.       symout_function (STMT_BODY (stmt), 0, address);
  869.     }
  870.       stmt = TREE_CHAIN (stmt);
  871.     }
  872.   return address;
  873. }
  874.  
  875. /* Output all the data structure for a top two blocks in a compilation.
  876.    The top block is for public (global) symbols;
  877.    the next one is for private (this file only) symbols.
  878.  
  879.    DECLS is the chain of declarations of variables in this block.
  880.    TAGS is the list of struct, union and enum tag definitions.  */
  881.  
  882. void
  883. symout_top_blocks (decls, tags)
  884.      tree decls;
  885.      tree tags;
  886. {
  887.   register tree decl;
  888.   register int i;
  889.   register int *addr_buffer;
  890.   struct block buffer;
  891.   int n_decls, n_tags;
  892.   register struct blockvec_elt *velt;
  893.   int top_block_addr;
  894.  
  895.   /* First do the public-symbols block.  */
  896.  
  897.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  898.     if (TREE_PUBLIC (decl) && ! TREE_EXTERNAL (decl))
  899.       i++;
  900.   n_decls = i;
  901.  
  902.   addr_buffer = (int *) alloca (n_decls * sizeof (int));
  903.  
  904.   symout_block_symbols (decls, addr_buffer, 2);
  905.  
  906.   fprintf (asmfile, ".text 0\n\t.gdbend 0\n");
  907.   fprintf (asmfile, "\t.gdbblock 0,%d\n", next_address);
  908.  
  909.   total_blocks++;
  910.   velt = (struct blockvec_elt *) xmalloc (sizeof (struct blockvec_elt));
  911.   velt->next = blockvec;
  912.   velt->address = next_address;
  913.   blockvec = velt;
  914.  
  915.   top_block_addr = next_address;
  916.  
  917.   buffer.startaddr = 0;
  918.   buffer.endaddr = 0;
  919.   buffer.superblock = 0;
  920.   buffer.function = 0;
  921.   buffer.nsyms = n_decls;;
  922.  
  923.   fwrite (&buffer, sizeof buffer - sizeof buffer.sym, 1, symfile);
  924.   next_address += sizeof buffer - sizeof buffer.sym;
  925.  
  926.   fwrite (addr_buffer, sizeof (int), n_decls, symfile);
  927.   next_address += n_decls * sizeof (int);
  928.  
  929.   /* Next do the private-symbols block.  */
  930.  
  931.   for (decl = decls, i = 0; decl; decl = TREE_CHAIN (decl))
  932.     if (! TREE_PUBLIC (decl) && ! TREE_EXTERNAL (decl))
  933.       i++;
  934.   n_decls = i;
  935.  
  936.   for (decl = tags, i = 0; decl; decl = TREE_CHAIN (decl), i++);
  937.   n_tags = i;
  938.  
  939.   addr_buffer = (int *) alloca ((n_decls + n_tags) * sizeof (int));
  940.  
  941.   symout_block_symbols (decls, addr_buffer, 1);
  942.   symout_block_tags (tags, addr_buffer + n_decls);
  943.  
  944.   fprintf (asmfile, "\t.gdbend 1\n");
  945.   fprintf (asmfile, "\t.gdbblock 1,%d\n", next_address);
  946.  
  947.   total_blocks++;
  948.   velt = (struct blockvec_elt *) xmalloc (sizeof (struct blockvec_elt));
  949.   velt->next = blockvec;
  950.   velt->address = next_address;
  951.   blockvec = velt;
  952.  
  953.   buffer.startaddr = 0;
  954.   buffer.endaddr = 0;
  955.   buffer.superblock = (struct block *) top_block_addr;
  956.   buffer.function = 0;
  957.   buffer.nsyms = n_decls + n_tags;;
  958.  
  959.   fwrite (&buffer, sizeof buffer - sizeof buffer.sym, 1, symfile);
  960.   next_address += sizeof buffer - sizeof buffer.sym;
  961.  
  962.   fwrite (addr_buffer, sizeof (int), n_decls + n_tags, symfile);
  963.   next_address += (n_decls + n_tags) * sizeof (int);
  964. }
  965.  
  966. /* Output the source-line-number information.  */
  967.  
  968. /* Output a `struct source' for the source file described by F.
  969.    Return the address-in-the-symseg of the `struct source'.  */
  970.  
  971. static int
  972. symout_source_file (f)
  973.      struct gdbfile *f;
  974. {
  975.   /* Make the `struct source' big enough for as many lines as
  976.      this file has.  */
  977.   int size = sizeof (struct source) + (f->nlines - 1) * sizeof (struct line);
  978.   struct source *buffer
  979.     = (struct source *) alloca (size);
  980.   int addr;
  981.  
  982.   /* Use zero for the line data, since assembler will store the real data.  */
  983.   bzero (buffer, size);
  984.  
  985.   /* Output the file's name as a string.  The assembler doesn't know this.  */
  986.   buffer->name = (char *) next_address;
  987.   symout_strings (f->name, 0, 0, 0);
  988.   buffer->nlines = f->nlines;
  989.  
  990.   /* Write the structure.  */
  991.   addr = next_address;
  992.   fwrite (buffer, 1, size, symfile);
  993.   next_address += size;
  994.  
  995.   /* Tell assembler where to write the real line-number data.  */
  996.   fprintf (asmfile, "\t.gdblinetab %d,%d\n",
  997.        f->filenum, addr + sizeof (int));
  998.  
  999.   return addr;
  1000. }
  1001.  
  1002. /* Output the `struct sourcevector' which describes all the
  1003.    source files and points a `struct source' for each one.  */
  1004.  
  1005. static int
  1006. symout_sources ()
  1007. {
  1008.   register struct gdbfile *f;
  1009.   int nfiles = 0;
  1010.   struct sourcevector *s;
  1011.   int i;
  1012.   int size;
  1013.   int addr;
  1014.  
  1015.   /* Count number of files to determine size of the sourcevector.  */
  1016.   for (f = gdbfiles; f; f = f->next)
  1017.     ++nfiles;
  1018.  
  1019.   /* Allocate buffer for the sourcevector and record its length.  */
  1020.   size = sizeof (int) + nfiles * sizeof (struct source *);
  1021.   s = (struct sourcevector *) alloca (size);
  1022.   s->length = nfiles;
  1023.  
  1024.   /* Output a `struct source' for each file; put address into sourcevector.  */
  1025.   for (f = gdbfiles, i = 0; f; f = f->next, i++)
  1026.     s->source[i] = (struct source *) symout_source_file (f);
  1027.  
  1028.   /* Output the sourcevector.  */
  1029.   addr = next_address;
  1030.   fwrite (s, 1, size, symfile);
  1031.   next_address += size;
  1032.   return addr;
  1033. }
  1034.  
  1035. /* Call here at the end of compilation, after outputting all the
  1036.    blocks and symbols, to output the blockvector and typevector
  1037.    and close the symbol table file.  FILETIME is source file's
  1038.    creation time.  */
  1039.  
  1040. void
  1041. symout_finish (filename, filetime)
  1042.      char *filename;
  1043.      int filetime;
  1044. {
  1045.   int *blockvector = (int *) alloca ((total_blocks + 1) * sizeof (int));
  1046.   int *typevector = (int *) alloca ((total_types + 1) * sizeof (int));
  1047.   int now = time (0);
  1048.   register int i;
  1049.   struct symbol_root buffer;
  1050.   char dir[MAXNAMLEN];
  1051.  
  1052.   buffer.language = language_c;
  1053.   buffer.blockvector = (struct blockvector *) next_address;
  1054.  
  1055.   /* The two blocks at the beginning of the chain
  1056.      are the file's private symbols block and public symbols block.
  1057.      They belong at the front of the blockvector, in that order.  */
  1058.   blockvector[2] = blockvec->address;
  1059.   blockvec = blockvec->next;
  1060.   blockvector[1] = blockvec->address;
  1061.   blockvec = blockvec->next;
  1062.  
  1063.   /* The rest of the blocks are in the chain in reverse order.  */
  1064.   for (i = total_blocks; i > 2; i--)
  1065.     {
  1066.       blockvector[i] = blockvec->address;
  1067.       blockvec = blockvec->next;
  1068.     }
  1069.   blockvector[0] = total_blocks;
  1070.  
  1071.   fwrite (blockvector, sizeof (int), total_blocks + 1, symfile);
  1072.   next_address += sizeof (int) * (total_blocks + 1);
  1073.  
  1074.   buffer.typevector = (struct typevector *) next_address;
  1075.  
  1076.   for (i = total_types; i > 0; i--)
  1077.     {
  1078.       typevector[i] = typevec->address;
  1079.       typevec = typevec->next;
  1080.     }
  1081.   typevector[0] = total_types;
  1082.  
  1083.   fwrite (typevector, sizeof (int), total_types + 1, symfile);
  1084.   next_address += sizeof (int) * (total_types + 1);
  1085.  
  1086.   buffer.sourcevector = (struct sourcevector *) symout_sources ();
  1087.  
  1088.   buffer.format = 1;
  1089.   buffer.textrel = 0;        /* These four will be set up by linker.  */
  1090.   buffer.datarel = 0;        /* Make them 0 now, which is right for */
  1091.   buffer.bssrel = 0;        /* looking at the .o file in gdb.  */
  1092.   buffer.ldsymoff = 0;
  1093.  
  1094.   buffer.version = (char *) next_address;
  1095.   symout_strings (ctime (&filetime), 0, 0, 0);
  1096.  
  1097.   buffer.compilation = (char *) next_address;
  1098.   symout_strings (ctime (&now), 0, 0, 0);
  1099.  
  1100.   buffer.filename = (char *) next_address;
  1101.   symout_strings (filename, 0, 0, 0);
  1102.  
  1103.   buffer.filedir = (char *) next_address;
  1104. #ifdef USG
  1105.   strcpy (dir, getcwd (dir, MAXNAMLEN));
  1106. #else
  1107. #ifndef VMS
  1108.   getwd (dir);
  1109. #else
  1110.   abort ();
  1111. #endif
  1112. #endif
  1113.   symout_strings (dir, 0, 0, 0);
  1114.  
  1115.   fflush (symfile);
  1116.  
  1117.   if (ferror (symfile) != 0)
  1118.     fatal_io_error (symfile_name);
  1119.  
  1120.   buffer.length = next_address;
  1121.  
  1122.   if (lseek (fileno (symfile), 0, 0) < 0)
  1123.     pfatal_with_name (symfile_name);
  1124.   if (write (fileno (symfile), &buffer, sizeof buffer) < 0)
  1125.     pfatal_with_name (symfile_name);
  1126.   close (fileno (symfile));
  1127. }
  1128.